package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.entity.Top10;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ReportMapper;
import com.sky.mapper.UserMapper;
import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public TurnoverReportVO turnoverReport(LocalDate begin, LocalDate end) {
       //根据返回的数据格式创建所需要的对象

        //分装所需要的日期集合
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)){
            begin = begin.plusDays(1);//日期计算，获得指定日期后1天的日期
            dateList.add(begin);
        }
        //分装所需要的营业额集合
        List<Double> turnoverList = new ArrayList<>();
        for(LocalDate d : dateList){
            LocalDateTime dayBegin = LocalDateTime.of(d, LocalTime.MIN);
            LocalDateTime dayEnd = LocalDateTime.of(d, LocalTime.MAX);
            Map map = new HashMap();
            map.put("status", Orders.COMPLETED);
            map.put("begin", dayBegin);
            map.put("end", dayEnd);
            Double turnover = orderMapper.getTurnover(map);
            turnover = turnover == null ? 0.0 : turnover;
            turnoverList.add(turnover);
        }
        return TurnoverReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .turnoverList(StringUtils.join(turnoverList,","))
                .build();
    }

    @Override
    public UserReportVO userReport(LocalDate begin, LocalDate end) {

        //日期数组分装
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)){
            begin = begin.plusDays(1);
            dateList.add(begin);
        }

        //新用户数据分装
        List<Integer> newUserList = new ArrayList<>();
        for(LocalDate d : dateList){
            LocalDateTime dayBegin = LocalDateTime.of(d, LocalTime.MIN);
            LocalDateTime dayEnd = LocalDateTime.of(d, LocalTime.MAX);
            Map map = new HashMap();
            map.put("begin", dayBegin);
            map.put("end", dayEnd);
            Integer count = userMapper.getUserCount(map);
            newUserList.add(count);
        }

        //用户总数的数据分装
        List<Integer> userList = new ArrayList<>();
        for(LocalDate d : dateList){
            LocalDateTime dayEnd = LocalDateTime.of(d, LocalTime.MAX);
            Map map = new HashMap();
            map.put("end", dayEnd);
            Integer sum = userMapper.getUserCount(map);
            userList.add(sum);
        }

        return UserReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .newUserList(StringUtils.join(newUserList,","))
                .totalUserList(StringUtils.join(userList,","))
                .build();

    }

    /**
     * 订单统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public OrderReportVO getOrderReport(LocalDate begin, LocalDate end) {
        //分装日期数组
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)){
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        //分装每日订单数，有效订单数
        List<Integer> ordersList = new ArrayList<>();
        List<Integer> totalOrdersList = new ArrayList<>();
        for(LocalDate d : dateList){
            LocalDateTime dayBegin = LocalDateTime.of(d, LocalTime.MIN);
            LocalDateTime dayEnd = LocalDateTime.of(d, LocalTime.MAX);
            Map map = new HashMap();
            map.put("begin", dayBegin);
            map.put("end", dayEnd);
            Integer total =  orderMapper.getOrdersNumber(map);
            map.put("static",Orders.COMPLETED);
            Integer effectOrders = orderMapper.getOrdersNumber(map);
            totalOrdersList.add(total);
            ordersList.add(effectOrders);
        }
        //获得订单总数和有效订单数
        Map map = new HashMap<>();
        Integer totalOrderCount = orderMapper.getOrdersNumber(map);
        map.put("status",Orders.COMPLETED);
        Integer validOrderCount = orderMapper.getOrdersNumber(map);

        Double orderCompletionRate = (double)validOrderCount / totalOrderCount;
        return OrderReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .validOrderCountList(StringUtils.join(ordersList,","))
                .orderCountList(StringUtils.join(totalOrdersList,","))
                .validOrderCount(validOrderCount)
                .totalOrderCount(totalOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .build();

    }

    @Override
    public SalesTop10ReportVO getSalesTop10Report(LocalDate begin, LocalDate end) {
        LocalDateTime dayBegin = LocalDateTime.of(begin, LocalTime.MIN);
        //nameList
        List<String> nameList = new ArrayList<>();
        LocalDateTime dayEnd = LocalDateTime.of(end, LocalTime.MAX);
        //numberList
        List<Integer> numberList = new ArrayList<>();
        List<Top10> list = orderMapper.getSalesTop10Report(dayBegin,dayEnd);
        for(Top10 t : list){
            nameList.add(t.getName());
            numberList.add(t.getCount());
        }
        return SalesTop10ReportVO.builder()
                .nameList(StringUtils.join(nameList,","))
                .numberList(StringUtils.join(numberList,","))
                .build();
    }
}
